<template>
	<view class="pageview">
		<cl-navbar autoBack placeholder :title="$t('chat.group_chat_config.title')"></cl-navbar>
		<view class="pageContent">
			<span class="classType">基础配置</span>
			<view class="cellviewGrop">
				<!-- 默认群 -->
				<view class="cellview">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.default_qun")}}</text>
					</view>
					<view class="cellview__right">
						<u-switch v-model="groupInfo.default_qun" size="20" disabled
							:activeValue="1" :inactiveValue="0" inactiveColor="#121325"
							activeColor="#121325"></u-switch>
					</view>
				</view>
				
				<!-- 允许谁邀请进群 -->
				<view class="cellview"  @click="editInfo('is_invitation','radio',$t('chat.group_chat_config.is_invitation'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.is_invitation")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.is_invitation]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			<span class="classType">显示权限</span>
			<view class="cellviewGrop">
				<!-- 能够退群的人 -->
				<view class="cellview" @click="editInfo('can_quit_range','radio',$t('chat.group_chat_config.can_quit_range'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.can_quit_range")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.can_quit_range]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
				<!-- 允许谁添加好友 -->
				<view class="cellview"  @click="editInfo('can_add_friends','radio',$t('chat.group_chat_config.can_add_friends'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.can_add_friends")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.can_add_friends]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
				<!-- 显示在线人数 -->
				<view class="cellview" @click="editInfo('show_online_number','radio',$t('chat.group_chat_config.show_online_number'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.show_online_number")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.show_online_number]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
				<!-- 显示群总人数 -->
				<view class="cellview" @click="editInfo('show_qun_number','radio',$t('chat.group_chat_config.show_qun_number'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.show_qun_number")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.show_qun_number]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
				<!-- 显示ip -->
				<view class="cellview" @click="editInfo('show_ip','radio',$t('chat.group_chat_config.show_ip'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.show_ip")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.show_ip]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			
			<span class="classType">消息权限</span>
			<view class="cellviewGrop">
				<!-- 展示管理消息 -->
				<view class="cellview" @click="editInfo('show_sys_msg','radio',$t('chat.group_chat_config.show_sys_msg'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.show_sys_msg")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.show_sys_msg]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
				<!-- 谁可以编辑消息 -->
				<view class="cellview" @click="editInfo('can_edit_msg','radio',$t('chat.group_chat_config.can_edit_msg'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.can_edit_msg")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.can_edit_msg]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
				
				<!-- 查看历史消息 -->
				<!-- <view class="cellview" @click="editInfo('show_history','radio',$t('chat.group_chat_config.show_history'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.show_history")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.show_history]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view> -->
				<!-- 删除退群人消息 -->
				<!-- <view class="cellview" @click="editInfo('can_delete_quit_msg','radio',$t('chat.group_chat_config.can_delete_quit_msg'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.can_delete_quit_msg")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.can_delete_quit_msg]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view> -->
				<!-- 允许撤回自己消息 -->
				<view class="cellview" @click="editInfo('can_withdraw_self_msg','radio',$t('chat.group_chat_config.can_withdraw_self_msg'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.can_withdraw_self_msg")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.can_withdraw_self_msg]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
				<!-- 允许撤回他人消息 -->
				<!-- <view class="cellview" @click="editInfo('can_withdraw_other_msg','radio',$t('chat.group_chat_config.can_withdraw_other_msg'))">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("chat.group_chat_config.can_withdraw_other_msg")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.can_withdraw_other_msg]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view> -->
			</view>
			<!-- <view class="cellviewGrop" @click="editInfo('can_send_card','radio','允许发送名片')">
				<view class="cellview">
					<view class="cellview__left">
						<text class="cellview__left__title">允许发送名片</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{configData[groupInfo.can_send_card]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view> -->
		</view>
		<view>
			<cl-edit-pop ref="clEditPop" :title="editInfoData.title" @complete="popComplete" @cancel="popCancel">
				<view class="cellviewGrop" v-for="(item,key) in configData" :key="key" @click="setConfig(key)">
					<view class="cellview" >
						<view class="cellview__left">
							<text class="cellview__left__title">{{item}}</text>
						</view>
						<view class="cellview__right">
							<image src="/static/icon/select.png" mode="" style="width: 36rpx;height: 36rpx;" v-if="editInfoData.data[editInfoData.model] == key"></image>
							<image src="/static/icon/no-select.png" mode="" style="width: 36rpx;height: 36rpx;" v-else></image>
						</view>
					</view>
				</view>
				
				<!-- <view style="padding:30rpx;">
					<u-input v-if="editInfoData.type=='radio'"
						:customStyle="{backgroundColor:'#1E1F30',height:'110rpx',padding:'0 30rpx',borderRadius: '20rpx'}"
						fontSize="28rpx" color="#ffffff" focus :adjustPosition="false" border="none"
						v-model="editInfoData.data[editInfoData.model]"></u-input>
				</view> -->
			</cl-edit-pop>
		</view>
	</view>
</template>

<script>
	import sqliteGroup from '@/sqlite_services/group.js'
	import sqliteSeesion from '@/sqlite_services/seesion.js'
	import upload from '@/common/upload/upload.js'
	import groupApi from '@/services/api/group.js'
	import {
		mapState
	} from 'vuex'
	export default {
		name: 'group_chat_config',
		data() {
			return {
				editInfoData: {
					type: '',
					title: '',
					model: '',
					data: {}
				},
				configData:{
					0:this.$t("chat.group_chat_config.possessor"),
					1:this.$t("chat.group_chat_config.ordinaryUser"),
					2:this.$t("chat.group_chat_config.manage"),
					3:this.$t("chat.group_chat_config.banAll"),
				},
			}
		},
		async onLoad() {
			console.log(this.groupInfo);
		},
		computed: {
			...mapState({
				groupInfo: state => state.chat.chatGroupInfo,
			}),
		},
		methods: {
			editInfo(type, inputType, title) {
				let infodata = JSON.parse(JSON.stringify(this.groupInfo))
				let {
					group_id,
					is_review,
					is_invitation,
					show_sys_msg,
					show_online_number,
					show_member_list,
					show_ip,
					show_history,
					show_qun_number,
					can_delete_quit_msg,
					can_withdraw_self_msg,
					can_withdraw_other_msg,
					can_send_card,
					can_quit_range,
					can_add_friends,
					can_edit_msg,
					is_muted
				} = infodata
				
				this.editInfoData = {
					type: inputType,
					title: title,
					model: type,
					data: {
						group_id,
						is_review,
						is_invitation,
						show_sys_msg,
						show_online_number,
						show_member_list,
						show_ip,
						show_history,
						show_qun_number,
						can_delete_quit_msg,
						can_withdraw_self_msg,
						can_withdraw_other_msg,
						can_send_card,
						can_edit_msg,
						can_quit_range,
						can_add_friends,
						is_muted
					}
				}
				this.$nextTick(() => {
					this.$refs.clEditPop.show()
				})
			},
			setConfig(key){
				this.editInfoData.data[this.editInfoData.model] = key
			},
			popComplete() {
				this.apiUpdate(this.editInfoData.data)
			},
			popCancel() {
				this.editInfoData = {
					type: '',
					title: '',
					model: '',
					data: {}
				}
			},
			async apiUpdate(data) {
				try{
					let resapi = await groupApi.editGroupConf(data)
					if (resapi) {
						this.$refs.clEditPop.cancel()
						uni.showToast({
							title: resapi.msg,
							icon: 'none'
						})
						return true
					}else{
						return false
					}
				}catch(e){
					throw e
				}
			},
			async setChange(type, val) {
				let {
					group_id,
					is_review,
					is_invitation,
					show_sys_msg,
					show_online_number,
					show_member_list,
					show_ip,
					show_history,
					show_qun_number,
					can_delete_quit_msg,
					can_withdraw_self_msg,
					can_withdraw_other_msg,
					can_send_card,
					can_quit_range,
					can_add_friends,
					is_muted
				} = this.groupInfo
				switch (type) {
					case 'is_review':
						//这里需要更新用户表和会话表
						let data = {
							group_id,
							is_review: val,
							is_invitation,
							show_sys_msg,
							show_online_number,
							show_member_list,
							show_ip,
							show_history,
							show_qun_number,
							can_delete_quit_msg,
							can_withdraw_self_msg,
							can_withdraw_other_msg,
							can_send_card,
							can_quit_range,
							can_add_friends,
							is_muted,
							group_id:this.groupInfo.group_id,
						}
						let apiok = await this.apiUpdate(data)
						if(apiok){
							this.groupInfo.is_review = val
						}
						break;
					default:
						break;
				}
			},
		},
	};
</script>

<style scoped lang="scss">
	.topInfo {
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 20rpx 30rpx 0;
		padding: 30rpx;

		&__numview {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28rpx;
			color: #FFFFFF;

			&__right {
				display: flex;
				align-items: center;

				&__num {
					font-size: 28rpx;
					color: #61626e;
				}
			}
		}

		&__view {
			display: flex;
			margin-top: 30rpx;
			gap: 10rpx;

			&__item {
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 120rpx;

				&__nickname {
					font-size: 24rpx;
					color: #FFFFFF;
					margin-top: 20rpx;
				}
			}

		}
	}
	.classType{
		font-size: 24rpx;
		color: #60606d;
		margin: 0 30rpx ;
	}

	.cellviewGrop {
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 10rpx 30rpx 20rpx;
		
		min-height: 110rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		justify-content: center;

		.cellview {
			display: flex;
			justify-content: space-between;
			padding: 30rpx;
			&__left {
				display: flex;
				align-items: center;

				&__title {
					font-size: 28rpx;
					color: #FFFFFF;
				}
			}

			&__right {
				display: flex;
				align-items: center;

				&__content {
					font-size: 28rpx;
					color: #61626e;
				}
			}
		}

		.cellDetails {
			font-size: 28rpx;
			color: #61626e;
			margin-top: 20rpx;
		}
	}

	.controlsBotton {
		height: 110rpx;
		text-align: center;
		line-height: 110rpx;
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 20rpx 30rpx 50rpx;
		font-size: 32rpx;
		color: #FF5252;
	}
</style>